﻿Imports DTO
Public Class HoaDon_DAO

#Region "Lập hoá đơn"
    Public Shared Function LapHoaDon(ByVal hoadon As HoaDon_DTO) As Integer
        Dim sql As String = String.Format("insert into HoaDon(MaKH,NgayLap,TongTien) values({0},'{1}',{2})", hoadon.MaKH, hoadon.NgayLap, hoadon.TongTien)
        Dim flag As Integer = DataProvider.ExcuteNonQuery(sql)
        sql = String.Format("update KhachHang set TienNo = TienNo + {0} where MaKH = {1}", hoadon.TongTien, hoadon.MaKH)
        flag = DataProvider.ExcuteNonQuery(sql)
        Return flag
    End Function
#End Region

#Region "Nhập chi tiết hoá đơn"
    Public Shared Function NhapChiTietHoaDon(ByVal cthoadon As CT_HoaDon_DTO) As Integer
        Dim sql As String = String.Format("insert into CT_HoaDon(MaHoaDon,MaSach,SoLuong,DonGia,ThanhTien) values({0},{1},{2},{3},{4})", cthoadon.MaHD, cthoadon.MaSach, cthoadon.SoLuongMua, cthoadon.DonGia, cthoadon.ThanhTien)
        Dim flag As Integer = DataProvider.ExcuteNonQuery(sql)
        sql = String.Format("update Sach set SoLuong = SoLuong - {0} where MaSach = {1}", cthoadon.SoLuongMua, cthoadon.MaSach)
        flag = DataProvider.ExcuteNonQuery(sql)
        Return flag
    End Function
#End Region

#Region "Load Hóa đơn và tên + CMND khách hàng"
    Public Shared Function LoadHoaDon() As DataTable
        Dim sql As String = "select hd.MaHoaDon,kh.TenKH,kh.CMND,hd.NgayLap,hd.TongTien from KhachHang kh,HoaDon hd where kh.MaKH = hd.MaKH"
        Return DataProvider.ExcuteReader(sql)
    End Function
#End Region

#Region "Load chi tiết hóa đơn theo mã HD"
    Public Shared Function LoadPhieuNhapTheoMa(ByVal ma As Integer) As DataTable
        Dim sql As String = String.Format("select s.TenSach,ct.SoLuong,ct.DonGia,ct.ThanhTien from Sach s,CT_HoaDon ct where ct.MaSach = s.MaSach and ct.MaHoaDon = {0}", ma)
        Return DataProvider.ExcuteReader(sql)
    End Function
#End Region

End Class
